﻿using System;
using System.Data;
using System.Data.SqlServerCe;
using System.Net.Configuration;
using LDataAccess;
using LDriver;
using LDriver.Protocols;
using Models;
using Models.Collections;

namespace LModels.Proxy
{
    public class Proxy : ProxyClass
    {

    private IDbConnection conn;

    public pWorkspace Workspace;
    public pSurvey Survey;
    public pRecording Recording;
    public pMeasurement Measurement;

    public Proxy()
    {
        conn = new SqlCeConnection(@"Data Source=|DataDirectory|\database.sdf");
        Workspace = new pWorkspace(conn);
        Survey = new pSurvey(conn);
        Recording = new pRecording(conn);
        Measurement = new pMeasurement(conn);
    }


    public IDbConnection GetConnection()
    {
        return conn;
    }

        public class pMeasurement : ProxySubClass
        {
            public pMeasurement(IDbConnection conn) : base(conn)
            {
            }

            public override IDataReader List()
            {
                return (IDataReader)ExecuteReader("MeasurementList", conn);
            }

            public override IDataReader ListById(long ID)
            {
                throw new NotImplementedException();
            }

            public override IDataReader Load(long IDMeasurement)
            {
                return (IDataReader)ExecuteReader("MeasurementLoad", conn, IDMeasurement);
            }

            public override void Delete(long IDMeasurement)
            {
                ExecuteNonQuery("MeasurementDelete", conn, IDMeasurement);
            }
        }




    public class pWorkspace : ProxySubClass
    {
        public pWorkspace(IDbConnection conn) : base(conn)
        {
        }

        public override IDataReader ListById(long ID)
        {
            return null;
        }

        public override IDataReader List()
        {
            return (IDataReader)ExecuteReader("WorkspaceList", conn);
        }

        public override IDataReader Load(long IDWorkspace)
        {
            return (IDataReader)ExecuteReader("WorkspaceLoad", conn, IDWorkspace);
        }

        public long? Save(Workspace work)
        {
            long ret;
            long.TryParse(ExecuteScalar("WorkspaceInsert", conn, work.Date, work.Description, work.Place, work.Notes).ToString(), out ret);
            return ret;
        }

        public void Update(Workspace work)
        {
            ExecuteNonQuery("WorkspaceUpdate", conn, work.Date, work.Description, work.Notes, work.Place, work.ID);
        }

        public override void Delete(long IDWorkspace)
        {
            ExecuteNonQuery("WorkspaceDelete", conn, IDWorkspace);
        }

      }

    public class pSurvey : ProxySubClass
    {
        public pSurvey(IDbConnection conn) : base(conn)
        {
        }

        public override IDataReader ListById(long ID)
        {
            return null;
        }

        public override IDataReader List()
        {
            return ExecuteReader("SurveyList", conn);
        }

        public IDataReader ListByRecording(long IDRecording)
        {
            return ExecuteReader("SurveyListByRecording", conn);
        }

        public override IDataReader Load(long IDSurvey)
        {
            return ExecuteReader("SurveyLoad", conn, IDSurvey);
        }

        public override void Delete(long ID)
        {
            throw new NotImplementedException();
        }

        public void Save(Recording rec, ISurvey survey)
        {
            ExecuteNonQuery("SurveySave", conn, rec.IDRecordings, survey.ToString());
        }

        public void SaveBulk(long idrec, SurveysCollection recordingSurveys)
        {
            DataTable dt = new DataTable("Surveys");
            dt.Columns.Add("IDSurvey", typeof(long));
            dt.Columns.Add("IDRecording", typeof(long));
            dt.Columns.Add("Value", typeof(string));

            for (int i = 0; i < recordingSurveys.Count; i++)
                dt.Rows.Add(new object[] {i + 1, idrec, ((Survey)recordingSurveys[i]).survey.ToString()});

            DataTableReader dr = new DataTableReader(dt);

            ExecuteBulkInsert(dr, "Surveys");
        }
    }

    public class pRecording : ProxySubClass
    {
        public pRecording(IDbConnection conn) : base(conn)
        {
        }

        public override IDataReader List()
        {
            return null;
        }

        public override IDataReader ListById(long ID)
        {
            return ExecuteReader("RecordingsListByWorkspace", conn, ID);
        }

        public override IDataReader Load(long IDRecording)
        {
            return ExecuteReader("RecordingsList", conn, IDRecording);
        }

        public override void Delete(long ID)
        {
            throw new NotImplementedException();
        }

        public void Save(Recording recording)
        {
            //ExecuteNonQuery("RecordingsSave", conn, recording.Datetime, recording.IDWorkspace, recording.Provider.Description, recording.IDProtocol,recording.Notes);


     //       INSERT INTO [Recordings]
     //      ([Date]
     //      ,[Command]
     //      ,[IDWorkspace]
     //      ,[Provider]
     //      ,[Protocol]
     //      ,[Note])
     //VALUES
     //      (@Date,
     //      @Command,
     //      @IDWorkspace,
     //      @Provider,
     //      @Protocol,
     //      @Note);
     }
    }

}

}
